﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class PrintController : BaseController
    {
        /// <summary>
        /// Declare Print Repository
        /// </summary>
        private PrintRepository printRepository;

        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionPrintSort = "SessionPrintSort";
        public static string SessionPrintSearch = "SessionPrintSearch";
        public static string SessionPrintSortOpt = "SessionPrintSortOpt";



        // GET: /Print/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public PrintController()
            : base()
        {
            this.printRepository = new PrintRepository();
            ViewBag.SelectedMenuId = 25;
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Kế Hoạch In";
            // Stored Session

            Session[SessionPrintSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionPrintSort];
            Session[SessionPrintSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionPrintSortOpt];

            var print = this.printRepository.ListPrints(false).ToList();

            // Search
            if (Session[SessionPrintSearch] != null)
            {
                ViewBag.Keyword = Session[SessionPrintSearch].ToString();

                print = print.Where(l => l.Name.ToUpper().Contains(ViewBag.Keyword.ToUpper())).ToList();

            }
            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionPrintSort] != null)
            {
                switch (Session[SessionPrintSort].ToString())
                {
                    case "name":
                        print = Session[SessionPrintSortOpt].ToString().Equals("desc") ? print.OrderByDescending(s => s.Name).ToList() : print.OrderBy(s => s.Name).ToList();

                        Session[SessionPrintSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionPrintSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(print.ToPagedList(page, size));
        }
    }
}
    